package com.tiendung.otalunch;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class LocationActivity extends MapActivity
{
	private TextView tvName;
	private TextView tvAddress;
	private TextView tvOpenTime;
	
	private static RestaurantLocation restaurant;
	
	private static MapController mc;
	private static MapView mapView;
	
	private static double current_lat;
	private static double current_long;
	
	private static MyLocationListener mNetworkListener;
	private static String restaurant_name;
	private static boolean wasUpdated = false;
	
	private class MyLocationListener implements LocationListener 
    {	
		Context ct;
		
		public MyLocationListener(Context ct) {
			this.ct = ct;
		}
		
		@Override
		public void onLocationChanged(Location location) {
			makeUseOfNewLocation(location);
		}

		@Override
		public void onProviderDisabled(String provider) {}

		@Override
		public void onProviderEnabled(String provider) {}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {}
		
		public void makeUseOfNewLocation(Location location) {
			current_lat = location.getLatitude();
			current_long = location.getLongitude();
			
			if (!wasUpdated)
			{
				GeoPoint p = restaurant.getCenter(current_lat, current_long);        	 
		        
				mc.animateTo(p);
		        mc.setZoom(12);
		           
				List<Overlay> mapOverlays = mapView.getOverlays();				
		        // Drawable end_drawable = ct.getResources().getDrawable(R.drawable.end);
		        Drawable start_drawable = ct.getResources().getDrawable(R.drawable.start);
		        
		        LocationItemOverlay start_itemizedoverlay = new LocationItemOverlay(start_drawable, ct);                 
		        GeoPoint currentLocation = restaurant.getCurrentLocation(current_lat, current_long);
		        if (currentLocation != null)
		        {
			        OverlayItem start_overlayitem = new OverlayItem(currentLocation, "Start", "Your current location");                    
			        start_itemizedoverlay.addOverlay(start_overlayitem);                  
			        mapOverlays.add(start_itemizedoverlay);  
		        }
		        
		        mapView.postInvalidate();   
		        wasUpdated = true;
			}
		}
    };
    
    @Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location);		
		restaurant_name = getIntent().getStringExtra("Name");
				
		tvName = (TextView) findViewById(R.id.restaurant); 
		tvAddress = (TextView) findViewById(R.id.address); 
		tvOpenTime = (TextView) findViewById(R.id.opentime); 
		
		tvName.setText(restaurant_name);		
		tvAddress.setText(Restaurant.getAddress(restaurant_name));
		tvOpenTime.setText("Lunch time: " + Restaurant.getOpenTime(restaurant_name, "Monday"));
		
		mapView = (MapView) findViewById(R.id.mapview);
		mc = mapView.getController();
 
		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);	
		mNetworkListener = new MyLocationListener(this.getApplicationContext());
		
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mNetworkListener);
		mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mNetworkListener);
				
		restaurant = new RestaurantLocation(restaurant_name);		

		GeoPoint p = restaurant.getCenter(current_lat, current_long);
        mc.animateTo(p);
        mc.setZoom(15);         
        
        List<Overlay> mapOverlays = mapView.getOverlays();                  
        Drawable end_drawable = getResources().getDrawable(R.drawable.end);
        Drawable start_drawable = getResources().getDrawable(R.drawable.start);
        
        LocationItemOverlay start_itemizedoverlay = new LocationItemOverlay(start_drawable, this);                 
        GeoPoint currentLocation = restaurant.getCurrentLocation(current_lat, current_long);
        if (currentLocation != null)
        {
	        OverlayItem start_overlayitem = new OverlayItem(currentLocation, "Start", "Your current location");                    
	        start_itemizedoverlay.addOverlay(start_overlayitem);                  
	        mapOverlays.add(start_itemizedoverlay);  
        }
        
        LocationItemOverlay end_itemizedoverlay = new LocationItemOverlay(end_drawable, this);                 
        GeoPoint destination = restaurant.getLocation();
        OverlayItem end_overlayitem = new OverlayItem(destination, "Destination", restaurant_name);                    
        end_itemizedoverlay.addOverlay(end_overlayitem);                  
        mapOverlays.add(end_itemizedoverlay); 
        
        mapView.invalidate();
	}
	
	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}
}
